package com.chen.authserver.v1.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class TokenRedisRepository {

    private static final String KEYPREX = "AUTH_TOKEN_";

    private static final int TTL = (1 * 60 * 1000);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void save(String account, String token) {
        String realKey = getRealKey(account);
        stringRedisTemplate.opsForValue().set(realKey, token, TTL, TimeUnit.MICROSECONDS);
    }

    public void delete(String account) {
        String realKey = getRealKey(account);
        stringRedisTemplate.delete(realKey);
    }

    public String get(String account) {
        String realKey = getRealKey(account);
        return stringRedisTemplate.opsForValue().get(realKey);
    }

    private String getRealKey(String account) {
        return KEYPREX + account;
    }
}
